<div class="doc-item" id="doc-1328"><h2 class="doc-item-name">sh<span class="namespace">(clojure.contrib.shell-out)</span></h2><ul class="var-arglists"><li>(<span class="var-name">sh</span> &amp; args)</li></ul><div class="var-docstring"><p>Passes the given strings to Runtime.exec() to launch a sub-process.</p><p>Options are</p><p>:in    may be given followed by a String specifying text to be fed to the 
         sub-process's stdin.  
  :out   option may be given followed by :bytes or a String. If a String 
         is given, it will be used as a character encoding name (for 
         example &quot;UTF-8&quot; or &quot;ISO-8859-1&quot;) to convert the 
         sub-process's stdout to a String which is returned.
         If :bytes is given, the sub-process's stdout will be stored in 
         a byte array and returned.  Defaults to UTF-8.
  :return-map
         when followed by boolean true, sh returns a map of
           :exit =&gt; sub-process's exit code
           :out  =&gt; sub-process's stdout (as byte[] or String)
           :err  =&gt; sub-process's stderr (as byte[] or String)
         when not given or followed by false, sh returns a single
         array or String of the sub-process's stdout followed by its
         stderr
  :env   override the process env with a map (or the underlying Java
         String[] if you are a masochist).
  :dir   override the process dir with a String or java.io.File.</p><p>You can bind :env or :dir for multiple operations using with-sh-env
  and with-sh-dir.</p></div><div class="var-source"><pre>; clojure/contrib/shell_out.clj:82
(defn sh
  [&amp; args]
  (let [opts (parse-args args)
        proc (.exec (Runtime/getRuntime) 
		    (into-array (:cmd opts)) 
		    (as-env-string (:env opts))
		    (as-file (:dir opts)))
        in-stream (.getInputStream proc)]
    (when (:in opts)
      (with-open [osw (OutputStreamWriter. (.getOutputStream proc))]
        (.write osw (:in opts))))
    (let [stdout (.getInputStream proc)
          stderr (.getErrorStream proc)
          [[out err] combine-fn]
            (if (= (:out opts) :bytes)
              [(for [strm [stdout stderr]]
                (into-array Byte/TYPE (map byte (stream-seq strm))))
               #(aconcat Byte/TYPE %1 %2)]
              [(for [strm [stdout stderr]]
                (apply str (map char (stream-seq 
                                       (InputStreamReader. strm (:out opts))))))
                 str])
           exit-code  (.waitFor proc)]
      (if (:return-map opts)
        {:exit exit-code :out out :err err}
        (combine-fn out err)))))</pre></div><div class="license"><p>Copyright (c) Rich Hickey. All rights reserved.</p><p>The use and distribution terms for this software are covered by the <a href="http://opensource.org/licenses/eclipse-1.0.php">Eclipse Public License 1.0</a>, which can be found in the file
          epl-v10.html at the root of this distribution. By using this software
          in any fashion, you are agreeing to be bound by the terms of this
          license. You must not remove this notice, or any other, from this
          software.</p></div></div>